package com.lg.baselibrary.utils;

import android.app.Activity;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * Created by ligang on 2017/5/31.
 * description：监听软键盘的显示和隐藏
 */

public class KeyBoardChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {
    private View mContentView;
    private KeyBoardListener mKeyBoardListen;
    private int mOriginHeight;
    private int mPreHeight;
    private float statusbarHeight = 0.0F;
    public KeyBoardChangeListener(Activity activity)
    {
        if (activity == null) {
            Log.i("ListenerHandler", "contextObj is null");
        }else{
            this.mContentView = findContentView(activity);
            addContentTreeObserver();
        }
    }
    private void addContentTreeObserver(){
        this.mContentView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }
    private View findContentView(Activity activity) {
        try{
            this.statusbarHeight = getStatusbarHeight(activity);
        }catch (Exception localException){

        }
        return activity.getWindow().getDecorView().findViewById(android.R.id.content);
    }
    private int getStatusbarHeight(Activity activity){
        int i = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (i > 0) {
            return  activity.getResources().getDimensionPixelSize(i);
        }else{
            final Rect rect = new Rect();
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
            int n = rect.top;
            if (n != 0) {
                return n;
            }
            try {
                final Class<?> forName = Class.forName("com.android.internal.R$dimen");
                n = activity.getResources().getDimensionPixelSize(Integer.parseInt(forName.getField("status_bar_height").get(forName.newInstance()).toString()));
                return n;
            }catch (Exception ex) {
                return n;
            }
        }
    }
    private boolean keyboardShow;
    private   boolean isKeyboardShown(View rootView) {
        final int softKeyboardHeight = 50;
        Rect r = new Rect();
        rootView.getWindowVisibleDisplayFrame(r);
        DisplayMetrics dm = rootView.getResources().getDisplayMetrics();
        int heightDiff = rootView.getBottom() - r.bottom;
        return heightDiff > softKeyboardHeight * dm.density;
    }
    public void setKeyBoardListener(KeyBoardListener paramKeyBoardListener){
        this.mKeyBoardListen = paramKeyBoardListener;
    }

    @Override
    public void onGlobalLayout() {
        if(isKeyboardShown(mContentView)){
            if(!keyboardShow){
                keyboardShow = true;
                if(mKeyBoardListen != null){
                    mKeyBoardListen.onKeyboardChange(true);
                }
            }
        }else{
            keyboardShow = false;
            if(mKeyBoardListen != null){
                mKeyBoardListen.onKeyboardChange(false);
            }
        }
        Log.d("KeyBoardChangeListener", "statusbarHeight:" + statusbarHeight);
    }

    public  interface KeyBoardListener{
        void onKeyboardChange(boolean isShowKeyboard);
    }
}
